package com.hrserviceserve.controller;

import com.hrserviceserve.entity.Result;
import com.hrserviceserve.entity.User;
import com.hrserviceserve.service.UserService;
import com.hrserviceserve.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api(tags = "登录注册接口")
public class UserController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody User bodyUser){
        User user=userService.login(bodyUser);
        if (user==null){
            return Result.build(null,201,"登录失败，请检查账号密码");
        }else {
            Map<String,Object> map=new HashMap<>();
            String token= TokenUtils.getToken(user.getUsername());
            map.put("token",token);
            return Result.success(map);
        }
    }
    @ApiOperation(value = "获取用户信息")
    @GetMapping("/info")
    public Result info(@RequestParam("token") String token){
        String username=TokenUtils.getUsername(token);
        System.out.println(username);
        User info = userService.info(username);
        if (info!=null){
            Map<String,Object> map=new HashMap<>();
            map.put("name",info.getUsername());
            map.put("roles",info.getRole());
            map.put("avatar",info.getAvatar());
            map.put("personnelId",info.getPersonnelId());
            map.put("department",info.getDepartment());
            return Result.success(map);
        }else {
            return Result.build(null,50008,null);
        }
    }

    @PostMapping("/logout")
    public Result logout(){
        return Result.success();
    }
    @GetMapping("/upava")
    public Result upava(@RequestParam("username")String username,@RequestParam("avatar")String avatar){
        userService.upava(username,avatar);
        return Result.success();
    }
    @GetMapping("/uppwd")
    public Result uppwd(@RequestParam("username") String username,@RequestParam("pwd") String pwd){
        userService.uppwd(username,pwd);
        return Result.success();
    }
}
